package com.link2room.ironhide.util.image;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.link2room.common.util.StringUtil;
import org.springframework.core.io.InputStreamSource;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by LN on 2016/11/25.
 */
public class ImgCompress {
    private static String PIC_FORMAT = "jpg";

    @JsonIgnore
    private transient BufferedImage img;
    private int width;
    private int height;
    private byte[] imgByte;
    private String imageFileHashCode;

    public ImgCompress(){

    }

    public ImgCompress( byte[] imgByte){
        this.imgByte = imgByte;
    }
    /**
     * 构造函数
     */
    public ImgCompress(String fileName) throws IOException {
        File file = new File(fileName);// 读入文件

        img = ImageIO.read(file);      // 构造Image对象
        width = img.getWidth();    //  得到源图宽
        height = img.getHeight();  // 得到源图长
    }
    /**
     * 构造函数
     */
    public ImgCompress(InputStream fileInputStream) throws IOException {
        img = ImageIO.read(fileInputStream);      // 构造Image对象
        width = img.getWidth();    //  得到源图宽
        height = img.getHeight();  // 得到源图长
    }

    /**
     * 构造函数
     */
    public ImgCompress(InputStreamSource fileInputStream) throws IOException {
        img = ImageIO.read(fileInputStream.getInputStream());      // 构造Image对象
        width = img.getWidth();    //  得到源图宽
        height = img.getHeight();  // 得到源图长
    }

    public byte[] getImgByte() throws IOException {
        if(imgByte != null){
            return imgByte;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        if(ImageIO.write(img,PIC_FORMAT,out)){
            imgByte = out.toByteArray();
            return imgByte;
        }else{
            return new byte[0];
        }
    }

    public String getImageFileHashCode() throws IOException, NoSuchAlgorithmException {
        if(StringUtil.isBlank(imageFileHashCode)){
            return createImageFileHashCode();
        }else{
            return imageFileHashCode;
        }
    }
    private String createImageFileHashCode() throws IOException, NoSuchAlgorithmException {
        byte[] bytes = getImgByte();
        ByteArrayInputStream is = new ByteArrayInputStream(bytes);
        MessageDigest complete = MessageDigest.getInstance("MD5");
        try{
            int numRead;
            byte[] buffer = new byte[1024];
            do {
                numRead = is.read(buffer);
                if (numRead > 0) {
                    complete.update(buffer, 0, numRead);
                }
            } while (numRead != -1);
        }finally {
            is.close();
        }
        byte[] b = complete.digest();
        imageFileHashCode = "";
        for (int i=0; i < b.length; i++) {
            imageFileHashCode += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
        }
        return imageFileHashCode;
    }

    public int getHeight() {
        return height;
    }

    public BufferedImage getImg() throws IOException {
        if(img!=null){
            return img;
        }else{
            InputStream sbs = new ByteArrayInputStream(getImgByte());
            img = ImageIO.read(sbs);
            return img;
        }
    }

    public int getWidth() {
        return width;
    }
}
